﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Globalization;

using BizElements.Core.ProviderComponents;

namespace BizElements.Core.Oracle
{
    sealed class OracleContainsPredicateRenderer : IContainsPredicateRenderer
    {
        #region Render as CONTAINS function.

        public void Render(ContainsPredicate contains, DbmsType dbms, StringBuilder output, DbParameterCollection parameters)
        {
            output.Append("(");
            if (contains.Negate)
                output.Append("NOT CONTAINS(");
            else
                output.Append("CONTAINS(");

            contains.PredicateItems[0].Render(dbms, output, parameters);
            output.Append(", ");

            IDbColumn field = (IDbColumn)contains.PredicateItems[0].Item;
            string term = (string)(contains.PredicateItems[1].Item as DbParameter).Value;
            if (contains.TermType == ContainsTermType.PrefixTerm)
                term = '"' + term + '"';
            else
                term = '"' + term + "%\"";

            PredicateItem parametrizedTerm = new PredicateItem(term, DbType.String, field.MaxLengthIfText, field.GetPrefixedAlias());
            parametrizedTerm.Render(dbms, output, parameters);
            output.Append(")");
            output.Append(")");
        }

        #endregion
    }
}